import {createRouter,createWebHashHistory} from 'vue-router'
import Token from '../store/token'

import Layout from '@/views/layout/index.vue'


// Vue.use(Router)
const routes = [
    {
        path : '/',
        component : Layout,
        children : [
            {
                path : "index",
                component : ()=> import('@/views/index.vue'),
                meta : {
                    title : "首页"
                }
            },
            

            {
                path : "user/profile",
                component : ()=> import('@/views/user/profile/profile.vue'),
                meta : {
                    title : '个人中心'
                }
            },
        ]
    },
    {
        path : '/admin',
        component : Layout,
        children : [
            {
                path : "manager",
                component : ()=> import('@/views/manager/lists.vue'),
                meta : {
                    title : "管理员列表"
                }
            },
            {
                path : "role",
                component : ()=> import('@/views/role/lists.vue'),
                meta : {
                    title : '角色列表'
                }
            },
            {
                path : "route",
                component : ()=> import('@/views/route/lists.vue'),
                meta : {
                    title : '路由'
                }
            },
        ]
    },
    {
        path : '/common',
        component : Layout,
        children : [
            {
                path : "setting",
                component : ()=> import('@/views/setting/lists.vue'),
                meta : {
                    title : "设置"
                }
            },
            {
                path : "category",
                component : ()=> import('@/views/category/lists.vue'),
                meta : {
                    title : "分类"
                }
            },
        ]
    },
    {
        path : '/content',
        component : Layout,
        children : [
            {
                path : "adGroup",
                component : ()=> import('@/views/adGroup/lists.vue'),
                meta : {
                    title : "轮播图组"
                }
            },
            {
                path : "ad",
                component : ()=> import('@/views/ad/all.vue'),
                meta : {
                    title : "轮播图"
                }
            },
            {
                path : "notice",
                component : ()=> import('@/views/notice/lists.vue'),
                meta : {
                    title : "公告"
                }
            },
        ]
    },
    {
        path : '/auth',
        children:[
            {
                name :'login',
                path : "login",
                meta : {requireLogin:false,},
                component : ()=> import('@/views/auth/login/login.vue')
            },
            {
                name :'logout',
                path : "logout",
                component : ()=> import('@/views/auth/logout/logout.vue')
            },
        ]
    },
    
    {
        path : "/:catchAll(.*)",
        meta : {requireLogin:false,},
        component : ()=>import('@/views/errors/404.vue')
    }
]
const router = createRouter({
    // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    history: createWebHashHistory(),
    routes, // `routes: routes` 的缩写
})

const setTitle = (route)=>{
    if(route?.meta?.title){
        document.title = route.meta.title;
    }
}

// 全局捕捉404
router.beforeEach((to,from,next)=>{
    if(to?.meta?.requireLogin !== false){
        // 必须要登录的页面
        Token().getToken()
        .then(res=>{
            setTitle(to)
            next()
        })
        .catch(err=>{
            next({name:'login',query : {
                redirect : from.path
            }})
        })
    }else{
        setTitle(to)
        next()
    }
})

// router.beforeResolve(async to=>{

// })

export default router;